<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tpshop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: IT宇宙人 2015-08-10 $
 *
 * TPshop为照顾新手，让新手们都能读懂代码，将代码写的比较通俗易懂，不写太过高深代码。
 */ 
namespace app\home\controller; 
use think\Controller;
use think\Url;
use think\Config;
use think\Page;
use think\Verify;
use think\Db;
class Index extends Base {

    
    public function index(){      

        // 如果是手机跳转到 手机模块
        if(true == isMobile()){
            header("Location: ".U('Mobile/Index/index'));
        }
        
        
        
        $hot_goods = $hot_cate = $cateList = array();
        $sql = "select a.goods_name,a.goods_id,a.shop_price,a.market_price,a.cat_id,b.parent_id_path,b.name from ".C('database.prefix')."goods as a left join ";
        $sql .= C('database.prefix')."goods_category as b on a.cat_id=b.id where a.is_hot=1 and a.is_on_sale=1 order by a.sort";//二级分类下热卖商品       
        $index_hot_goods = S('index_hot_goods');
        if(empty($index_hot_goods))
        {
            $index_hot_goods = Db::query($sql);//首页热卖商品
            S('index_hot_goods',$index_hot_goods,TPSHOP_CACHE_TIME);
        }
       
        if($index_hot_goods){
                foreach($index_hot_goods as $val){
                        $cat_path = explode('_', $val['parent_id_path']);
                        $hot_goods[$cat_path[1]][] = $val;
                }
        }
        $hot_category = M('goods_category')->where("is_hot=1 and level=3 and is_show=1")->cache(true,TPSHOP_CACHE_TIME)->select();//热门三级分类
        foreach ($hot_category as $v){
        	$cat_path = explode('_', $v['parent_id_path']);
        	$hot_cate[$cat_path[1]][] = $v;
        }
        
        foreach ($this->cateTrre as $k=>$v){
            if($v['is_hot']==1){
        		$v['hot_goods'] = empty($hot_goods[$k]) ? '' : $hot_goods[$k];
        		$v['hot_cate'] = empty($hot_cate[$k]) ? '' : $hot_cate[$k];
        		$cateList[] = $v;
        	}
        }
        $this->assign('cateList',$cateList);
        return $this->fetch();
    }
 
    /**
     *  公告详情页
     */
    public function notice(){
        return $this->fetch();
    }
    
    // 二维码
    public function qr_code(){        
        // 导入Vendor类库包 Library/Vendor/Zend/Server.class.php
        //http://www.tp-shop.cn/Home/Index/erweima/data/www.99soubao.com
         require_once 'ThinkPHP/Library/Vendor/phpqrcode/phpqrcode.php';
          //import('Vendor.phpqrcode.phpqrcode');
            error_reporting(E_ERROR);            
            $url = urldecode($_GET["data"]);
            \QRcode::png($url);          
    }
    
    // 验证码
    public function verify()
    {
        //验证码类型
        $type = I('get.type') ? I('get.type') : '';
        $fontSize = I('get.fontSize') ? I('get.fontSize') : '40';
        $length = I('get.length') ? I('get.length') : '4';
        
        $config = array(
            'fontSize' => $fontSize,
            'length' => $length,
            'useCurve' => true,
            'useNoise' => false,
        );
        $Verify = new Verify($config);
        $Verify->entry($type);        
    }
    
    // 促销活动页面
    public function promoteList()
    {
        $goodsList = DB::query("select * from __PREFIX__goods as g inner join __PREFIX__flash_sale as f on g.goods_id = f.goods_id   where ".time()." > start_time  and ".time()." < end_time");
        $brandList = M('brand')->getField("id,name,logo");
        $this->assign('brandList',$brandList);
        $this->assign('goodsList',$goodsList);
        return $this->fetch();
    }
    
    function truncate_tables (){
        $tables = DB::query("show tables");
        $table = array('tp_admin','tp_config','tp_region','tp_system_module','tp_admin_role','tp_system_menu','tp_article_cat');
        foreach($tables as $key => $val)
        {                                    
            if(!in_array($val['tables_in_tpshop'], $table))                             
                echo "truncate table ".$val['tables_in_tpshop'].' ; ';
                echo "<br/>";         
        }                
    }

    /**
     * 猜你喜欢
     * @author lxl
     * @time 17-2-15
     */
    public function ajax_favorite(){
        $p = I('p/d',1);
        $i = I('i',5); //显示条数
        $favourite_goods = M('goods')->where("is_recommend=1 and is_on_sale=1")->order('goods_id DESC')->page($p,$i)->cache(true,TPSHOP_CACHE_TIME)->select();//首页推荐商品
        $this->assign('favourite_goods',$favourite_goods);
        return $this->fetch();
    }
    
    /*
     * 我的分销明细
     */
    public function distribut_list(){
        $user_id = $_SESSION['user']['user_id'];
        if(empty($user_id)){
            echo "非法访问~!";
            exit();
        }
        $first_rate = tpCache("distribut.first_rate")/100;
        $second_rate = tpCache("distribut.second_rate")/100;
        if(I('post.sub_user_id')){//查询下线
            $sub_user_id = I('post.sub_user_id');
            $distrbut_info = get_distribut_income_detail($sub_user_id,$second_rate);//查询所有二级下线收益
            //查询下线用户信息
            $distrbut_info['user_info'] = M('users')->where('user_id',$sub_user_id)->find();
            $distrbut_info['user_info']['user_id'] = sprintf("%09d",$distrbut_info['user_info']['user_id']);
            $back['status'] = 1;
            $back['msg'] = "查询成功";
            $back['data'] = $distrbut_info;
            echo json_encode($back);
            exit;
        }
        if(I('post.type')){//根据查询条件筛选
           $starttime = empty(I("post.starttime"))?0:strtotime(I("post.starttime"));//默认0
           $endtime = empty(I("post.endtime"))?strtotime(date('Y-m-d',strtotime('+1 day'))):strtotime('+1 day',strtotime(I("post.endtime")));//默认明日凌晨
           $distribut_income = get_distribut_income_detail($user_id,$first_rate,$starttime,$endtime);//查询注册时间段内的收益详情
           if(!empty(I("post.sub_uid"))){//筛选出指定id的收益详情
               foreach ($distribut_income['distribut_income_detail'] as $k1=>$v1){
                    if($v1['user_id'] == I("post.sub_uid")){
                        $distribut_income1[] = $v1;
                    }
               }
               if(empty($distribut_income1)){
                   $distribut_income['distribut_income_detail'] = null;
               }else{
                   $distribut_income['distribut_income_detail'] = $distribut_income1;
               }
           }
           $back['status'] = 1;
           $back['msg'] = "查询成功";
           $back['data'] = $distribut_income;
           echo json_encode($back);
           exit;
        }
        //查询用户信息
        $user_info = M('users')->where('user_id',$user_id)->find();
        //统计用户一级下线
        $user_info['distribut_count'] = M('users')->where('first_leader',$user_id)->count();
        $distribut_income = get_distribut_income_detail($user_info['user_id'],$first_rate);//收益详情
        foreach ($distribut_income['distribut_income_detail'] as $key=>$value){
            $distribut_incom += $value['distribut_income'];//总收益
            $distribut_income_count+= $value['distribut_income_count'];//总预期收益
            $distribut_income_next = get_distribut_income_detail($value['user_id'],$second_rate);//二级下线收益
            foreach ($distribut_income_next['distribut_income_detail'] as $k=>$v){
                $distribut_incom += $v['distribut_income'];//总收益
                $distribut_income_count+= $v['distribut_income_count'];//总预期收益
            }
        }
        //今日成交单数
//         $distribut_info['distribut_order'] = M("order")->where("confirm_time>$starttime and confirm_time<$endtime and user_id=$user_id and pay_status=4")->count();
        $this->assign("distribut_incom",$distribut_incom);
        $this->assign("distribut_income_count",$distribut_income_count);
        $this->assign("user_info",$user_info);
        $this->assign("distribut_income",$distribut_income);//
        $this->assign("sever_time",date("Y-m-d",time()));//服务器时间
        return $this->fetch();
    }
}